#!/usr/bin/python

import random
import sqlite
import cgi
import cgitb; cgitb.enable()

# Get Form Data
form = cgi.FieldStorage()
docname = form['name'].value
username = form['username'].value
if username == "":
	username = "NoName"

# Connect to DB and make query
con = sqlite.connect('layoutTool.db')
cur = con.cursor()

query = "SELECT * FROM documents WHERE name='%s' AND username='%s'" % (docname, username)

cur.execute(query)

if cur.rowcount > 0:
	row = cur.fetchone()
	print "{\"id\": %s, \"username\":\"%s\", \"author\":\"%s\", \"title\":\"%s\", \"description\":\"%s\", \"json\":%s}" % (row['id'], row['username'], row['author'], row['title'], row['description'], row['json'])
else:
	print "No record found :("

con.close()
	